package com.wica.view.tab_preference;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.widget.Toast;

import com.wica.facetube.R;

public class TabPreferencesMapping extends TabPreferences{
	
	// *************** Constants ************** //  
    public static final String MAPPING_TAB_PREFERENCES_1 = "numberOfResults_mapping_preference";
    public static final String MAPPING_TAB_PREFERENCES_2 = "radius_mapping_preference";
    public static final int MAX_MAPPING_TAB_PREFERENCES_1=50;
    public static final int MAX_MAPPING_TAB_PREFERENCES_2=99;

	// *************** Methods **************************** //
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.mapping_preferences);    
    }// onCreate method    

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
    	// Local variables
		Integer integer;

		if (key.equals(MAPPING_TAB_PREFERENCES_1)) {
	    	integer = new Integer(sharedPreferences.getString(key, "0"));

        	if (integer.intValue()<MAX_MAPPING_TAB_PREFERENCES_1){
        		Toast.makeText(this, sharedPreferences.getString(key, "0")+" results will be shown.", Toast.LENGTH_SHORT).show();
        		finish();
        	}else if (integer.intValue()==MAX_MAPPING_TAB_PREFERENCES_1){
        		Toast.makeText(this, "The maximum number of results ("+MAX_MAPPING_TAB_PREFERENCES_1+") will be shown.", Toast.LENGTH_SHORT).show();
        		finish();
        	}else{
        		Editor s = sharedPreferences.edit();        		
        		s.putString(key, ""+MAX_MAPPING_TAB_PREFERENCES_1);        		
        		s.commit();
        	}            
        }else if (key.equals(MAPPING_TAB_PREFERENCES_2)) {
        	integer = new Integer(sharedPreferences.getString(key, "0"));

        	if (integer.intValue()<MAX_MAPPING_TAB_PREFERENCES_2){
        		Toast.makeText(this, "Results will be shown within a radius of "+sharedPreferences.getString(key, "0"), Toast.LENGTH_SHORT).show();
        		finish();
        	}else if (integer.intValue()==MAX_MAPPING_TAB_PREFERENCES_2){
        		Toast.makeText(this, "Results will be shown within the maximum radius ("+MAX_MAPPING_TAB_PREFERENCES_2+")", Toast.LENGTH_SHORT).show();
        		finish();
        	}else{
        		Editor s = sharedPreferences.edit();        		
        		s.putString(key, ""+MAX_MAPPING_TAB_PREFERENCES_2);        		
        		s.commit();
        	}      
        }
	}// onSharedPreferenceChanged method

}// TabPreferencesSearch class
